﻿using System;
using System.IO;
using System.Windows.Markup;
using System.Collections.Generic;
using Petra.Gis;
using Petra.Gis.Geometry.Support;
using Petra.Gis.RenderingServices.GDIPlus;
using Petra.Gis.RenderingServices.PlanarBase;
using Petra.Gis.ReferenceSystemServices;
using Petra.Gis.DataServices.SHP;
using Petra.Gis.DataServices.GDO.Sqlserver;

namespace testGridMap
{
  public static class testSHPAndRaster
  {


    public static void testSHPAndTransformedRaster()
    {


      BitmapMap map = new BitmapMap()
      {
        KeepNorthUp = true,
        Width = 800,
        Height = 400,
        ProjectedCoordinateSystem = (ProjectedCoordinateSystem)Petra.Gis.Environment.SearchSrsBy("EPSG", "32662", ""),
        //ProjectionExtent = new BoundingBox(18.797204785644, 49.5163167473756, 18.8294604740699, 49.5360093650922),
        ProjectionExtent = new BoundingBox(-180, -90, 180, 90),
        NominalScale = 500000,
        ApplyMapRangeAsSpatialFilter = true,
        Scale = 250000000
        //Scale = 25000
      };





      ShpConnection c = new ShpConnection()
      {
        ConnectionString =
        @"Data Source=C:\Program Files\MapWindow\MapWindow\Sample Projects\World\Shapefiles ; 
         CoordinateSystemAuthority=EPSG ;
         CoordinateSystemCode = 32662 ;
         CoordinateStorageUnits = projdegree ;
         TextEncoding = 1252",
         Name = "World data in SHP"
      };

      c.Open();

      // prepare command
      ShpSelectCommand cmd = new ShpSelectCommand();
      cmd.Connection = c;
      // this first to get smooth performance
      cmd.CommandType = System.Data.CommandType.TableDirect;
      // set valid command now command
      cmd.CommandText = "world_adm0";

      //prepare style
      Petra.Gis.RenderingServices.GDIPlus.Style stl = new Petra.Gis.RenderingServices.GDIPlus.Style()
      {
        Enabled = true,
        StyleScaleType = Petra.Gis.RenderingServices.PlanarBase.StyleScaleTypeEnum.ScaleIndependent,
        StyleComponents = new Petra.Gis.RenderingServices.PlanarBase.StyleComponents()
        {
          {"Color", System.Drawing.Color.Gray},
          {"LineWidth", 1},
          {"FillColor", System.Drawing.Color.FromArgb(128, 50, 50, 250)}
        }
      };

      //stl.Components.Add("Color", System.Drawing.Color.Gray);
      //stl.Components.Add("LineWidth", 1);
      //stl.Components.Add("FillColor", System.Drawing.Color.FromArgb(128, 50, 50, 250));


      //SurfaceRenderer g = new SurfaceRenderer(map, stl);

      SurfaceRenderer g = new SurfaceRenderer()
      {
        Style = stl,
        SpatialFieldName = "Geometry" //optional, if user want to limit renderer to only one field
      };

      CommandLegendNode l = map.Legend.CreateLegendNode(cmd, g);

      map.Legend.ChildNodes.Add(l);




      GdoSqlConnection c1 = new GdoSqlConnection()
      {
        ConnectionString =
        @"Data Source=localhost; 
         Initial Catalog=TEST_RASTRY;
				 Integrated Security = True;
         MultipleActiveResultSets=True",
         Name = "Forest raster maps"
      };

      c1.Open();

      // prepare command
      GdoSqlCommand cmd1 = new GdoSqlCommand();
      cmd1.Connection = c1;
      cmd1.CommandType = System.Data.CommandType.Text;
      cmd1.CommandText = "select * from [TEST_RASTRY].[dbo].[GR_PORS]";

      // prepare renderer
      GridRenderer gr = new GridRenderer()
      {
        Style = new Petra.Gis.RenderingServices.GDIPlus.Style(),
        SpatialFieldName = "Geometry" //optional, if user want to limit renderer to only one field
      };

      Petra.Gis.RenderingServices.PlanarBase.LegendNodeBase l1 = map.Legend.CreateLegendNode(cmd1, gr);

      map.Legend.ChildNodes.Add(l1);




      System.Drawing.Bitmap b = map.Draw();

      b.Save("c:\\temp\\shp-world.png", System.Drawing.Imaging.ImageFormat.Png);

    }

  }
}
